using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Prototype_Game_1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteManager spriteManager;
        
        // Backround
        Texture2D backgroundTexture;
        
        // Game Bar
        SpriteFont font;
        Texture2D circleToCollect;
        string circleToCollectName;
        int gameTimer;
        int milliSecsCounter;
        int gameStartedTime;

        // Circle Container
        List<string> circles = new List<string>();

        // Game states
        enum GameState { Start, InGame, GameOver };
        GameState currentGameState = GameState.Start;     

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = 960;
            graphics.PreferredBackBufferWidth = 1280;
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // Fill the Container
            circles.Add("black_circle");
            circles.Add("blue_circle");
            circles.Add("green_circle");
            circles.Add("grey_circle");
            circles.Add("orange_circle");
            circles.Add("purple_circle");
            circles.Add("red_circle");
            circles.Add("yellow_circle");

            // Load the Font
            font = Content.Load<SpriteFont>(@"fonts\score");
            // Create the sprite manager that is in charge of drawing the sprites
            spriteManager = new SpriteManager(this, font, circles);
            Components.Add(spriteManager);
            // Don't enable it just yet
            spriteManager.Enabled = false;
            spriteManager.Visible = false;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            backgroundTexture = Content.Load<Texture2D>(@"Images\grey-backround");
            // Choose the first circle to collect
            circleToCollect = chooseRandomCircleToCollect();
            
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Count 60 seconds and check if game is over
            milliSecsCounter = (int)gameTime.TotalGameTime.TotalMilliseconds - gameStartedTime;
            gameTimer = 60 - (milliSecsCounter / 1000);
            if (milliSecsCounter >= 60000 && currentGameState == GameState.InGame)
            {
                currentGameState = GameState.GameOver;
                spriteManager.Enabled = false;
                spriteManager.Visible = false;
            }

            // Check the game state
            switch (currentGameState)
            {
                case GameState.Start:
                    if (Keyboard.GetState().GetPressedKeys().Length > 0)
                    {
                        currentGameState = GameState.InGame;
                        spriteManager.Enabled = true;
                        spriteManager.Visible = true;
                        gameStartedTime = (int)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    break;
                case GameState.InGame:
                    break;
                case GameState.GameOver:
                    if (Keyboard.GetState().IsKeyDown(Keys.Enter))
                        Exit();
                    break;
            }
            
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            switch (currentGameState)
            {
                case GameState.Start:
                    GraphicsDevice.Clear(Color.AliceBlue);

                    // Draw text for intro splash screen
                    spriteBatch.Begin();
                    spriteBatch.Draw(backgroundTexture,
                        new Rectangle(0, 0, Window.ClientBounds.Width,
                        Window.ClientBounds.Height), null,
                        Color.White, 0, Vector2.Zero,
                        SpriteEffects.None, 0);
                    string text = "Collect the Circles According to the Chosen Color";
                    spriteBatch.DrawString(font, text,
                        new Vector2((Window.ClientBounds.Width / 2)
                        - (font.MeasureString(text).X / 2),
                        (Window.ClientBounds.Height / 2)
                        - (font.MeasureString(text).Y / 2) - 300),
                        Color.White);

                    string instructions = "Press Right Alt to Change the Basket's Direction";
                    spriteBatch.DrawString(font, instructions,
                        new Vector2((Window.ClientBounds.Width / 2)
                        - (font.MeasureString(text).X / 2),
                        (Window.ClientBounds.Height / 2)
                        - (font.MeasureString(text).Y / 2) - 150),
                        Color.White);

                    spriteBatch.Draw(Content.Load<Texture2D>(@"Images/waste-paper-basket-1"),
                    new Vector2((Window.ClientBounds.Width / 2)
                        - (font.MeasureString(text).X / 2) - 250,
                        (Window.ClientBounds.Height / 2)
                        - (font.MeasureString(text).Y / 2)),Color.White);

                    spriteBatch.Draw(Content.Load<Texture2D>(@"Images/waste-paper-basket-left-1"),
                    new Vector2((Window.ClientBounds.Width / 2)
                        - (font.MeasureString(text).X / 2) + 50,
                        (Window.ClientBounds.Height / 2)
                        - (font.MeasureString(text).Y / 2)), Color.White);

                    spriteBatch.Draw(Content.Load<Texture2D>(@"Images/waste-paper-basket-down-1"),
                    new Vector2((Window.ClientBounds.Width / 2)
                        - (font.MeasureString(text).X / 2) + 350,
                        (Window.ClientBounds.Height / 2)
                        - (font.MeasureString(text).Y / 2)), Color.White);

                    spriteBatch.Draw(Content.Load<Texture2D>(@"Images/waste-paper-basket-right-1"),
                    new Vector2((Window.ClientBounds.Width / 2)
                        - (font.MeasureString(text).X / 2) + 650,
                        (Window.ClientBounds.Height / 2)
                        - (font.MeasureString(text).Y / 2)), Color.White);

                    text = "(Press any key to begin)";
                    spriteBatch.DrawString(font, text,
                        new Vector2((Window.ClientBounds.Width / 2)
                        - (font.MeasureString(text).X / 2),
                        (Window.ClientBounds.Height / 2)
                        - (font.MeasureString(text).Y / 2) + 200),
                        Color.White);

                    spriteBatch.End();
                    break;

                case GameState.InGame:
                    GraphicsDevice.Clear(Color.White);
                    spriteBatch.Begin();

                    // Draw background image
                    spriteBatch.Draw(backgroundTexture,
                        new Rectangle(0, 0, Window.ClientBounds.Width,
                        Window.ClientBounds.Height), null,
                        Color.White, 0, Vector2.Zero,
                        SpriteEffects.None, 0);

                    spriteBatch.DrawString(font, "Time: " + gameTimer, new Vector2(1100, 10), Color.White);

                    spriteBatch.End();
                    break;

                case GameState.GameOver:
                    GraphicsDevice.Clear(Color.AliceBlue);

                    spriteBatch.Begin();
                    // Draw background image
                    spriteBatch.Draw(backgroundTexture,
                        new Rectangle(0, 0, Window.ClientBounds.Width,
                        Window.ClientBounds.Height), null,
                        Color.White, 0, Vector2.Zero,
                        SpriteEffects.None, 0);
                    string gameover = "Time's Up!";
                    spriteBatch.DrawString(font, gameover,
                        new Vector2((Window.ClientBounds.Width / 2)
                        - (font.MeasureString(gameover).X / 2),
                        (Window.ClientBounds.Height / 2)
                        - (font.MeasureString(gameover).Y / 2)),
                        Color.White);

                    gameover = "Your score: " + spriteManager.Score;
                    spriteBatch.DrawString(font, gameover,
                        new Vector2((Window.ClientBounds.Width / 2)
                        - (font.MeasureString(gameover).X / 2),
                        (Window.ClientBounds.Height / 2)
                        - (font.MeasureString(gameover).Y / 2) + 30),
                        Color.White);

                    gameover = "(Press ENTER to exit)";
                    spriteBatch.DrawString(font, gameover,
                        new Vector2((Window.ClientBounds.Width / 2)
                        - (font.MeasureString(gameover).X / 2),
                        (Window.ClientBounds.Height / 2)
                        - (font.MeasureString(gameover).Y / 2) + 60),
                        Color.White);

                    spriteBatch.End();
                    break;
            }
            base.Draw(gameTime);
        }
        private Texture2D chooseRandomCircleToCollect()
        {
            Random r = new Random();
            int chosenCircle = r.Next(0, 8);
            circleToCollectName = circles.ElementAt(chosenCircle);
            Texture2D chosenCircleSprite = Content.Load<Texture2D>(@"Images/" + circleToCollectName);
            return chosenCircleSprite;
        }
    }
}
